package com.example.ice.fittime.util;

import com.example.ice.fittime.entry.ArticleBanner;
import com.example.ice.fittime.entry.Banners;
import com.example.ice.fittime.entry.ExperienceShare;
import com.example.ice.fittime.entry.HotUser;
import com.example.ice.fittime.entry.Sticker;
import com.example.ice.fittime.entry.Topic;
import com.example.ice.fittime.entry.UserActivity;
import com.example.ice.fittime.entry.user.ActivityUserBean;
import com.example.ice.fittime.entry.user.ArticleBean;
import com.example.ice.fittime.entry.user.StatisticsBean;
import com.example.ice.fittime.entry.user.UserBean;
import com.example.ice.fittime.global.ClassType;
import com.example.ice.fittime.global.Constants;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
import rx.Observable;

/**
 * Created by ice on 2016/10/12.
 */

public class NetUtil {
    private static Retrofit.Builder builder = new Retrofit.Builder();
    private static CourseServer courseServer;
    private static ApiServer apiServer;
    public static ActivityServer activityServer;
    public static BannersServer bannersServer;
    public static StickerHotServer stickerServer;
    public static HotUserServer hotUserServer;

    static {
        apiServer = builder
                .baseUrl(Constants.API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
                .create(ApiServer.class);
        courseServer = builder
                .baseUrl(Constants.COURSE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(CourseServer.class);


        activityServer = builder.baseUrl(Constants.API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ActivityServer.class);
        bannersServer = builder.baseUrl(Constants.API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BannersServer.class);

        stickerServer =  builder.baseUrl(Constants.API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(StickerHotServer.class);

        hotUserServer = builder.baseUrl(Constants.API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HotUserServer.class);
    }

    public static <T> void getData(ClassType classType, Callback<T> callback, String... values ) {
        switch (classType) {
            case ArticleBanner:
                apiServer.getArticleBanner().enqueue((Callback<List<ArticleBanner>>) callback);
                break;
            case ExperienceShare:
                apiServer.getExperienceShare(values[0],values[1]).enqueue((Callback<List<ExperienceShare>>)callback);
                break;
            case UserActivity:
                if (values.length==4) {
                    activityServer.getUserActivitys(values[0],values[1],values[2],values[3]).enqueue((Callback<List<UserActivity>>) callback);
                }
                apiServer.getUserActivity(values[0], "10", "0").enqueue((Callback<List<UserActivity>>)callback);
                break;
            case Topic:
                apiServer.getTopic().enqueue((Callback<List<Topic>>) callback);
                break;

            case Banners:
                bannersServer.getBanners().enqueue((Callback<List<Banners>>) callback);
                break;
            case Sticker:
                stickerServer.getStickerHots().enqueue((Callback<List<Sticker>>) callback);
                break;
            case HotUser:
                hotUserServer.getHotUsers(values[0],values[1],values[2],values[3]).enqueue((Callback<List<HotUser>>) callback);
                break;
        }
    }

    interface ApiServer {
        @GET("/api/v2/articleBanners")
        Call<List<ArticleBanner>> getArticleBanner();

        @GET("/api/v2/articles?v2&offset=0&limit=30")
        Call<List<ExperienceShare>> getExperienceShare(@Query("timestamp") String timestamp,@Query("hot") String hot);

        @GET("/v3/activity/selected")
        Call<List<UserActivity>> getUserActivity(@Query("timestamp") String timestamp,@Query("limit") String limit,@Query("offset") String offset);

        @GET("/api/v2/front-pages/current")
        Call<List<Topic>> getTopic();

        @GET("/v3/activity/user/{user_id}")
        Observable<List<ActivityUserBean>> getActivityUserBean(@Path("user_id") int user_id, @Query("timestamp") long timestamp, @Query("limit") int limit, @Query("offset") int offset);

        @GET("/profile/{user_id}")
        Observable<UserBean> getUserBean(@Path("user_id") int user_id);

        @GET("/statistics/user/{user_id}")
        Observable<StatisticsBean> getStatisticsBean(@Path("user_id") int user_id);

        @GET("/api/v2/articles")
        Observable<List<ArticleBean>> getArticleBean(@Query("user_id") int user_id);
    }

    public static Observable<List<ArticleBean>> getArticleBean(int user_id) {
        return apiServer.getArticleBean(user_id);
    }

    public static Observable<UserBean> getUserBean(int user_id) {
        return apiServer.getUserBean(user_id);
    }

    public static Observable<List<ActivityUserBean>> getActivityUserObservable(int user_id, long timestamp, int limit, int offset) {
        return apiServer.getActivityUserBean(user_id, timestamp, limit, offset);
    }

    public static Observable<StatisticsBean> getStatisticsBeanObservable(int user_id) {
        return apiServer.getStatisticsBean(user_id);
    }

    interface CourseServer {

    }

    interface ActivityServer{
        @GET("/v3/activity/{type}")
        Call<List<UserActivity>> getUserActivitys(@Path("type") String type, @Query("timestamp") String timestamp,@Query("limit")String limit,@Query("offset") String offset);
    }
    interface BannersServer{
        @GET("/api/v2/banners")
        Call<List<Banners>> getBanners();
    }

    interface StickerHotServer{
        @GET("/v3/sticker/hot")
        Call<List<Sticker>> getStickerHots();
    }

    interface HotUserServer {
        @GET("/v3/{type}")
        Call<List<HotUser>> getHotUsers(@Path("type") String type, @Query("timestamp") String timestamp, @Query("limit")String limit, @Query("offset") String offset);

    }



}
